﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Extentions;
using Microsoft.EntityFrameworkCore;
using TrainingReminderSystem.Model;
using Z.EntityFramework.Plus;

namespace TrainingReminderSystem.Dal
{
    /// <summary>
    /// 一些底层数据访问的数据封装
    /// </summary>
    public partial class DbEntitiesHelper
    {
        #region 得到基础Db对象

        /// <summary>
        /// 是否已删除
        /// </summary>
        private const string PropertyNameIsDel = "IsDel";

        /// <summary>
        /// 得到基础Db对象
        /// </summary>
        /// <param name="disableAllFilters">是否去掉筛选,默认为false</param>
        /// <returns></returns>
        public static DbEntities GetDbEntities(bool disableAllFilters)
        {
            var db = new DbEntities();
            if (disableAllFilters == false)
            {
                EfFilterHelper.BuildFilterBool(db, PropertyNameIsDel, false, false);

                //手动写法
                //string filterName = GetFilterName(typeof(TblTest), "CreateEmployeeID");
                //List<int?> ids = new List<int?>() { 1, 2 };
                //System.Func<System.Linq.IQueryable<TblTest>, System.Linq.IQueryable<TblTest>> express = f =>
                //    f.Where(m => m.CreateEmployeeID == null || ids.Contains(m.CreateEmployeeID));
                //db.Filter(filterName, express);

                //Stopwatch sw = new Stopwatch();
                //sw.Start();
                ////EfFilterHelper.BuildFilterInt(db, "CreateEmployeeID", 1); //这个就可以注入当前用户信息
                //EfFilterHelper.BuildFilterIntList(db, "CreateEmployeeID", new List<int>() { 1, 2 });
                //sw.Stop();
                //Console.WriteLine(sw.ElapsedMilliseconds);
                //sw.Reset();
            }

            return db;
        }

        #endregion
    }
}
